IF EXISTS(SELECT name FROM sysobjects
      WHERE name = 'pInProdHistory_Get' AND type = 'P')
   DROP PROCEDURE pInProdHistory_Get
GO

CREATE PROCEDURE dbo.pInProdHistory_Get
	@vintProductId		INT,
	@vdatFromDate		DATETIME = NULL,
	@vdatToDate			DATETIME = NULL
AS


/*______________________ CONSTANT DECLARATIONS _________________________*/

  

/*______________________ VARIABLE DECLARATIONS _________________________*/

  
/*_______________________________ INIT _________________________________*/


/*________________________ VALIDATE PARAMETERS _________________________*/
	
/*_______________________________ MAIN _________________________________*/
	
	
	SELECT pd.Amount, 
		pp.Date, 
		pd.InputPrice, 
		TotalInputPrice = ISNULL(pd.InputPrice,0) * ISNULL(pd.Amount, 0),
		RowNumber = ROW_NUMBER()OVER(ORDER BY pp.Date DESC)
	FROM InProdDetail pd 
	
	INNER JOIN InProdPaper pp
	ON pd.InProdPaperId = pp.Id
	
	WHERE ProductId = @vintProductId
		AND ((@vdatFromDate IS NULL) OR (DATEDIFF(DAY, @vdatFromDate, pp.Date) >= 0))
		AND ((@vdatToDate IS NULL) OR (DATEDIFF(DAY, pp.Date, @vdatToDate) >= 0))
		
	ORDER BY pp.Date DESC

/*___________________________ ERROR HANDLER ____________________________*/



